//
//  SISSComplaintViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/8/22.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSComplaintViewController.h"
#import "SISSTextView.h"
#import "SISSUploadImageView.h"
#import "SISSTitleView.h"
#import "SISSNetworkingManager+Complain.h"
#import "SISSNetworkingManager+Comment.h"

@interface SISSComplaintViewController ()
@property (weak, nonatomic)  SISSTextView *textView;
@property (weak, nonatomic)  SISSTitleView *titleView;
@property (weak, nonatomic)  SISSUploadImageView *uploadImageView;
@property (weak, nonatomic)  UIButton *confirmButton;

@end
@implementation SISSComplaintViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"申诉";
}


/**
 点击按钮的操作
 */
- (void)confirmButtonClickAction {
    
    NSString *content = self.textView.text;
    if (content.length == 0) {
        [SISSTools showSVSuccessTitle:@"请输入申诉内容" andTime:1 andColor:YES];
        return;
    }
    
    [kNetworkNoUTF8Manager insertComplainAndSuggestionWithAcrId:self.acrId complainType:2 commentType:self.commentType  content:content images:self.uploadImageView.images success:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] != 200) {
            
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [SISSTools showSVErrorTitle:responseObject[@"message"] andTime:1 andColor:YES];
            }];
            return ;
        }
        
        if ([responseObject[@"code"] integerValue] == 200) {
            [SISSTools showSVSuccessTitle:@"提交成功" andTime:1 andColor:YES];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}

#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    SISSTextView *textView = [[SISSTextView alloc] init];
    textView.placeholder = @"请简单描述申诉事由。";
    [self.view addSubview:textView];
    self.textView = textView;
    
    
    SISSTitleView *titleView = [[SISSTitleView alloc] init];
    titleView.titleStr = @"选择上传截图说明问题";
    [self.view addSubview:titleView];
    self.titleView = titleView;
    
    SISSUploadImageView *uploadImageView = [[SISSUploadImageView alloc] init];
    uploadImageView.isHiddenTitle = YES;
    [self.view addSubview:uploadImageView];
    self.uploadImageView = uploadImageView;
    
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    [confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [confirmButton setBackgroundColor:MAIN_COLOR];
    [confirmButton addTarget:self action:@selector(confirmButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:confirmButton];
    self.confirmButton = confirmButton;
}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    CGFloat margin = 8.0;
    CGFloat textViewHeigh = 200.0;
    WeakSelf
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(weakSelf.view);
        make.height.mas_equalTo(textViewHeigh);
    }];
    
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.view);
        make.top.equalTo(weakSelf.textView.mas_bottom);
        make.height.mas_equalTo(5 * margin);
    }];
    
    [self.uploadImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.view);
        make.top.equalTo(weakSelf.titleView.mas_bottom);
    }];
    
    [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.view).offset(4 * margin);
        make.right.equalTo(weakSelf.view).offset(-4 * margin);
        make.top.equalTo(weakSelf.uploadImageView.mas_bottom).offset(5 * margin);
        make.height.mas_equalTo(5.5 * margin);
    }];
}

@end
